///////////////////////////////////////////////////////////////////////////////
//
// aluTermIoAttr.cc - class for terminal I/O
// 
// 1st release: A. Lusiani <a.lusiani@sns.it> April 2005
//

///////////////////////////////////////////////////////////////////////////////
//
// headers
//

extern "C" {
#include <stdio.h>
#include <time.h>
}

#include "aluClockTicks.h"

///////////////////////////////////////////////////////////////////////////////
//
// methods 
//

//--- default constructor
aluClockTicks::aluClockTicks(unsigned long nsSet)
{
  ns = nsSet;

  clock_getres(CLOCK_REALTIME, &timeStart);
  //--- assume resolution < second
  nsRes = timeStart.tv_nsec;

  clock_gettime(CLOCK_REALTIME, &timeStart);
}

aluClockTicks::~aluClockTicks()
{
}

void aluClockTicks::Reset()
{
  clock_gettime(CLOCK_REALTIME, &timeStart);
}

void aluClockTicks::SetTime(const struct timespec *time)
{
  timeStart = *time;
}

long aluClockTicks::GetTicks()
{
  struct timespec timeCurr;
  clock_gettime(CLOCK_REALTIME, &timeCurr);
  
  long secDiff = timeCurr.tv_sec - timeStart.tv_sec;
  long nsDiff = timeCurr.tv_nsec - timeStart.tv_nsec;

  return nsDiff/nsRes + secDiff*(static_cast<long>(1e9)/nsRes);
}
